﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TierApplication;
using System.Web.Security;
using TierService;
/// <summary>
///BasePage 的摘要说明
/// </summary>
public class BasePage : System.Web.UI.Page
{
    public string MoudleID = "";
    public static string TheTwoSF = "{0:0.00}";
    public static string TheFourSF = "{0:0.0000}";
    public static string TheFiveSF = "{0:0.00000}";
    public static String TheCustomCookieName = "TheCookieName";

    public static String ApplicationRoot = HttpContext.Current.Request.ApplicationPath;

    public String CurrentUserCode
    {
        get
        {
            return Page.User.Identity.Name;
        }
    }

    public String CurrentUserName
    {
        get
        {
            if (this.CurrentUser != null)
                return this.CurrentUser.UserName;
            else
                return "";
        }
    }

    private UserInfo _currentUser;
    public UserInfo CurrentUser
    {
        get
        {
            return _currentUser;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        base.Load += new EventHandler(this.PageBase_Load);
        base.Error += new EventHandler(this.PageBase_Error);
    }

    protected void PageBase_Error(object sender, EventArgs e)
    {
        Exception lastError = base.Server.GetLastError();
        string s = "<link rel=\"stylesheet\" href=\"/style.css\">";
        string str2 = s;
        s = str2 + "<h1>系统错误：</h1><hr/>系统发生错误， 该信息已被系统记录，请稍后重试或与管理员联系。<br/>错误地址： " + base.Request.Url.ToString() + "<br/>错误信息： <font class=\"ErrorMessage\">" + lastError.Message.ToString() + "</font><hr/><b>Stack Trace:</b><br/>" + lastError.ToString();
        base.Response.Write(s);
        base.Server.ClearError();
    }

    private void PageBase_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            if (this.Context.User.Identity.IsAuthenticated)
            {
                P_User mUser = new P_User();

                UserInfo mUserInfo = mUser.GetUserInfoByUserCode(this.CurrentUserCode);
                if (mUserInfo.UserCode != null)
                    _currentUser = mUserInfo;
                else
                    _currentUser = null;
                
                if (this.Session["UserInfo"] == null)
                {
                    SessionHelper.Add("UserInfo", mUserInfo);
                    //base.Response.Write("<script defer>location.reload();</script>");
                }
                //if ((this.MoudleID != "") && !mUser.HasPermissionID(this.PermissionID))
                //{
                //    base.Response.Clear();
                //    base.Response.Write(@"<script defer>window.alert('您没有权限进入本页！\n请重新登录或与管理员联系');history.back();</script>");
                //    base.Response.End();
                //}
            }
            else
            {
                FormsAuthentication.SignOut();
                this.Session.Clear();
                this.Session.Abandon();
                base.Response.Clear();
                base.Response.Write(@"<script defer>window.alert('您没有权限进入本页或当前登录用户已过期！\n请重新登录或与管理员联系！');parent.location='" + ApplicationRoot + "/Login.aspx';</script>");
                base.Response.End();
            }
        }
    }


    protected void loadCurrentUser()
    {
        String userID;
        userID = this.CurrentUserCode;
        //BusinessObjects.BO_P_User theUser = new BusinessObjects.BO_P_User(userID);
        //if (theUser.UserID != null)
        //    _currentUser = theUser;
        //else
        //    _currentUser = null;
    }


    public static string URLCombine(string uri1, string uri2)
    {
        uri1 = uri1.TrimEnd('/');
        uri2 = uri2.TrimStart('/');
        return string.Format("{0}/{1}", uri1, uri2);
    }

    // IsNumeric Function
    public static bool IsNumeric(object Expression)
    {
        // Variable to collect the Return value of the TryParse method.
        bool isNum;

        // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
        double retNum;

        // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
        // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
        isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
        return isNum;
    }

}